import Vue from 'vue';
import ViewUI from 'view-design';
import util from '../libs/util';
import VueRouter from 'vue-router';
import Cookies from 'js-cookie';
import { routers } from './router';
import store from '@/store';
import { Message } from 'view-design';
import { setStore, getStore } from '@/libs/storage';
import { getGnSet } from '@/api/qyManage.js';
import { showCompanyInfo } from '@/router/signIn';
Vue.use(VueRouter);

// 路由配置
const RouterConfig = {
    mode: 'hash',
    routes: routers
};

export const router = new VueRouter(RouterConfig);

router.beforeEach((to, from, next) => {
    var gnSet = {};
    var qxPageList = ['jyqxManage', 'jyqxsh'];
    var qrdjPageList = [];
    var residencePageList = ['residentList'];
    var archivesPageList = ['archiveTransferInfo'];
    var dataSwitchPageList = ['dataReporting']
    var allList = [];
    allList.push(...qxPageList, ...qrdjPageList, ...residencePageList, ...archivesPageList,...dataSwitchPageList);
    var flag = false;
    if (Cookies.get('userInfo') && util.oneOf(to.name, allList)) {
        getGnSet().then(res => {
            if (res.success && res.result) {
                gnSet = res.result;
                let userInfo=JSON.parse(Cookies.get('userInfo'))
                if (util.oneOf(to.name, qxPageList) && !gnSet.qxdjSwitch) {
                    flag = true;
                }
                if (util.oneOf(to.name, qrdjPageList) && !gnSet.qrdjSwitch) {
                    flag = true;
                }
                if (util.oneOf(to.name, residencePageList) && !gnSet.residenceSwitch) {
                    flag = true;
                }
                if (util.oneOf(to.name, archivesPageList) && !gnSet.archivesSwitch) {
                    flag = true;
                }
                if (util.oneOf(to.name, dataSwitchPageList) && !gnSet.dataSwitch && userInfo.resource != 1) {
                    flag = true;
                }
            }
            // if (flag) {
            //     // Message.error({
            //     //     duration: 5,
            //     //     content: '本年度此模块尚未开放，请耐心等待',
            //     //     closable: true
            //     // });
            //     next({
            //         name: 'no-open'
            //     });
            //     return;
            // }
            routerGo(to, from, next);
        });
    } else {
        routerGo(to, from, next);
    }
});

router.afterEach(to => {
    util.openNewPage(router.app, to.name, to.params, to.query);
    ViewUI.LoadingBar.finish();
    window.scrollTo(0, 0);
});

function routerGo(to, from, next) {
    ViewUI.LoadingBar.start();
    util.title(to.meta.title);
    var name = to.name;
    let currentName = Cookies.get('currentUser');
    // 白名单

    // var whiteList = ['login', 'regist', 'regist-result','agreement', 'relate', 'reset', 'proreset','scoreset','authorize', 'activation', 'sysLogin', 'eduLogin', 'proLogin'];
    var whiteList = ['login', 'agreement', 'reset', 'proreset', 'scoreset', 'authorize', 'activation', 'edulogin', 'prologin', 'SignInPage', 'showCompanyInfo','notuse', 'WebView'];
    var isInWhiteList = util.oneOf(name, whiteList);

    if (!Cookies.get('userInfo') && !isInWhiteList) {
        if (currentName == undefined || currentName == null || currentName == 'null') {
            // Cookies.set('currentUser', store.state.user.isRoute, {
            //     expires: 7,
            // })
            //
            // next({
            //     name: Cookies.get('currentUser')
            // });
            next({
                name: 'edulogin'
            });
        } else {
            // 判断是否已经登录且页面不在白名单
            next({
                name: currentName
            });
        }
    } else {
        util.toDefaultPage([...routers], name, router, next);
    }
    // else if (Cookies.get('userInfo') && name == currentName) {
    //     // 判断是否已经登录且前往的是登录页
    //     util.title();
    //
    //     next({
    //         name: 'edulogin'
    //     });
    // }
}
